package com.mutual.help.controller;

import com.mutual.help.entity.Comment;
import com.mutual.help.service.CommentService;
import com.mutual.help.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    //添加评论
    @RequestMapping("wx/addComment")
    @ResponseBody
    public Object addComment(@RequestBody Comment comment){
        comment.setAddDate(new Date());
        if (commentService.save(comment))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    //获取questionId下的所有评价
    @RequestMapping("wx/getQuestionComment")
    @ResponseBody
    public Object getQuestionComment(HttpServletRequest request){
        String questionId = request.getParameter("questionId");
        List<Comment> commentList = commentService.getQuestionComment(Integer.parseInt(questionId));
        return WeChatResponseUtil.ok(commentList);
    }

}
